package threadlocal;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

public class ThreadLocalTest01 {


    public static void main(String[] args) {
//        新建ThreadLocal类
        ThreadLocal<String> local1 = new ThreadLocal<>();
        ThreadLocal<String> local2 = new ThreadLocal<>();

//        新建随机数类
        Random random = new Random();

//        使用java8的Stream新建5个线程
        IntStream.range(0,5).forEach(a->new Thread(()->{
            local1.set(a+" " + random.nextInt(10));
            local2.set(a + "thread");
            System.out.println("线程和local值分别是 "+local1.get() + ". local2的值:" + local2.get());
            try{
                TimeUnit.SECONDS.sleep(1);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }).start());


    }
}
